package com.shinhye.elephantsafe;

import com.shinhye.elephantsafe.recevier.ESAdminReceiver;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class GuardSetup4Activity extends GuardSetupBaseActivity {

	private static final int RESULT_CODE = 100;
	private CheckBox cb_guard;
	private ComponentName componentName;
	private DevicePolicyManager dpm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide4);
		
		componentName = new ComponentName(GuardSetup4Activity.this, ESAdminReceiver.class);
		dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
		
		cb_guard = (CheckBox) findViewById(R.id.cb_guardthref);
		boolean runguard = getSP().getBoolean("RunGuard", false);
		if(runguard) {
			cb_guard.setText(R.string.guide_set4_text1_on);
			cb_guard.setChecked(true);
		} else {
			cb_guard.setText(R.string.guide_set4_text1_off);
			cb_guard.setChecked(false);
		}
		
		cb_guard.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked) {
					Editor editor = getSP().edit();
					editor.putBoolean("RunGuard", true);
					editor.commit();
					
					cb_guard.setText(R.string.guide_set4_text1_on);
					
					boolean actived = dpm.isAdminActive(componentName);
					if(!actived) {
						System.out.println(componentName.getClassName());
						Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
				        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
				        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Activate this Admin to protect your mobile.");
				        startActivityForResult(intent, RESULT_CODE);
					}
					
				} else {
					Editor editor = getSP().edit();
					editor.putBoolean("RunGuard", false);
					editor.commit();
					
					cb_guard.setText(R.string.guide_set4_text1_off);
				}
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == RESULT_CODE) {
			if(resultCode == RESULT_OK) {
				//假如在激活管理员页面选择不激活，应该设置“开启防盗保护”的CheckBox不选中。
//				boolean actived = dpm.isAdminActive(componentName);
//				if(!actived) {
//					cb_guard.setChecked(false);
//				}
//				Toast.makeText(this, "激活管理员帐号才能开启防盗保护！", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this, "激活管理员失败！无法开启防盗保护！", Toast.LENGTH_SHORT).show();
				cb_guard.setChecked(false);
			}
		}
	}

	@Override
	public void showNext() {
		Editor editor = getSP().edit();
		editor.putBoolean("skipguide", true);
		editor.commit();
		
		Intent intent = new Intent(this, GuardThrefActivity.class);
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.translate_next_in, R.anim.translate_next_out);
	}

	@Override
	public void showPrev() {
		Intent intent = new Intent(this, GuardSetup3Activity.class);
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.translate_prev_in, R.anim.translate_prev_out);
	}
}
